<template>
  <el-card class="box-card">
    <template #header>
      <div class="card-header">
        <span>市场概览</span>
        <div class="data-source-controls">
          <span class="last-updated">最后更新: {{ lastUpdated }}</span>
          <el-select :value="selectedDataSource" placeholder="选择数据源" size="small" style="margin-right: 10px;" @change="handleDataSourceChange">
            <el-option
              v-for="item in dataSources"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
          <el-button type="primary" size="small" @click="fetchDataBySource" :loading="isLoading">刷新</el-button>
          <el-switch
            :value="autoRefresh"
            active-text="自动刷新"
            inactive-text="手动刷新"
            @change="toggleAutoRefresh"
          />
        </div>
      </div>
    </template>
    <el-row :gutter="20">
      <el-col :span="6" v-for="(item, index) in marketOverview" :key="index">
        <div class="market-item" :class="{ 'up': item.change > 0, 'down': item.change < 0 }">
          <div class="market-name">{{ item.name }}</div>
          <div class="market-price">{{ item.price }}</div>
          <div class="market-change">
            <span>{{ item.change > 0 ? '+' : '' }}{{ item.change }}%</span>
            <el-icon v-if="item.change > 0"><CaretTop /></el-icon>
            <el-icon v-else-if="item.change < 0"><CaretBottom /></el-icon>
          </div>
        </div>
      </el-col>
    </el-row>
  </el-card>
</template>

<script>
import { ref, reactive } from 'vue'
import { CaretTop, CaretBottom } from '@element-plus/icons-vue'

export default {
  name: 'MarketOverview',
  components: {
    CaretTop,
    CaretBottom
  },
  props: {
    marketOverview: {
      type: Array,
      required: true
    },
    dataSources: {
      type: Array,
      required: true
    },
    selectedDataSource: {
      type: String,
      required: true
    },
    lastUpdated: {
      type: String,
      required: true
    },
    isLoading: {
      type: Boolean,
      required: true
    },
    autoRefresh: {
      type: Boolean,
      required: true
    }
  },
  emits: ['fetch-data-by-source', 'toggle-auto-refresh', 'update:selectedDataSource'],
  setup(props, { emit }) {
    const fetchDataBySource = () => {
      emit('fetch-data-by-source')
    }

    const toggleAutoRefresh = (value) => {
      emit('toggle-auto-refresh', value)
    }

    const handleDataSourceChange = (value) => {
      emit('update:selectedDataSource', value)
    }

    return {
      fetchDataBySource,
      toggleAutoRefresh,
      handleDataSourceChange
    }
  }
}
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.data-source-controls {
  display: flex;
  align-items: center;
  gap: 10px;
}

.last-updated {
  font-size: 12px;
  color: #909399;
  margin-right: 10px;
}

.market-item {
  padding: 15px;
  border-radius: 8px;
  background-color: #f5f7fa;
  text-align: center;
  margin-bottom: 10px;
}

.market-name {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 5px;
}

.market-price {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 5px;
}

.market-change {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 5px;
}

.up {
  color: #14b143;
}

.down {
  color: #ef232a;
}
</style>